import 'dart:convert';

import 'package:azlistview/azlistview.dart';
import 'package:chat/model/friend_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:lpinyin/lpinyin.dart';

class FriendListController extends GetxController {
  final RxList<Friend> friends = <Friend>[].obs;
  final RxList<Friend> groupChats = <Friend>[].obs;
  final RxString searchQuery = ''.obs;
  final RxString currentLetter = ''.obs;
  final ScrollController scrollController = ScrollController();
  var friendList = <Friend>[].obs;

  @override
  void onInit() {
    super.onInit();
    loadData();
  }

  @override
  void onClose() {
    scrollController.dispose();
    super.onClose();
  }

  void loadData() async {
    //加载联系人列表
    rootBundle.loadString('assets/data/car_models.json').then((value) {
      List list = json.decode(value);
      for (var v in list) {
        friendList.add(Friend.fromJson(v));
      }
      _handleList(friendList);
    });
  }

  void _handleList(List<Friend> list) {
    if (list.isEmpty) return;
    for (int i = 0, length = list.length; i < length; i++) {
      String pinyin = PinyinHelper.getPinyinE(list[i].name);
      String tag = pinyin.substring(0, 1).toUpperCase();
      list[i].namePinyin = pinyin;
      if (RegExp("[A-Z]").hasMatch(tag)) {
        list[i].tagIndex = tag;
      } else {
        list[i].tagIndex = "#";
      }
    }
    // A-Z sort.
    SuspensionUtil.sortListBySuspensionTag(friendList);
    // show sus tag.
    SuspensionUtil.setShowSuspensionStatus(friendList);
    friendList.insert(0, Friend(name: '搜索', tagIndex: '↑'));
    friendList.insert(1, Friend(name: '群聊', tagIndex: '↑'));
  }

  // void _loadFriends() {
  //   // 群聊数据
  //   groupChats.assignAll([
  //     Friend(id: 'group', name: '群聊', avatar: 'assets/group_avatar.png', identifier: ''),
  //   ]);

  //   // 好友数据 (根据图片中的信息)
  //   friends.assignAll([
  //     Friend(
  //       id: '1',
  //       name: 'boreditalinao',
  //       avatar: 'assets/boreditalinao_avatar.png',
  //       identifier: 'Oxf670..820d',
  //     ),
  //     Friend(
  //       id: '2',
  //       name: 'shzbbb',
  //       avatar: 'assets/shzbbb_avatar.png',
  //       identifier: 'Ox31f5...b0bc',
  //     ),
  //     Friend(
  //       id: '3',
  //       name: 'PacmanNFT',
  //       avatar: 'assets/pacman_avatar.png',
  //       identifier: 'Ox0838...61c5',
  //     ),
  //     Friend(
  //       id: '4',
  //       name: 'serendipito00',
  //       avatar: 'assets/serendipito00_avatar.png',
  //       identifier: 'Ox90b7...7e55',
  //     ),
  //     Friend(
  //       id: '5',
  //       name: 'djohnsonl',
  //       avatar: 'assets/djohnsonl_avatar.png',
  //       identifier: 'Oxc28d..5099',
  //     ),
  //     Friend(
  //       id: '6',
  //       name: 'Kingdodge',
  //       avatar: 'assets/kingdodge_avatar.png',
  //       identifier: 'Oxede4...jc8r',
  //     ),
  //     Friend(
  //       id: '7',
  //       name: 'giamdoxing',
  //       avatar: 'assets/giamdoxing_avatar.png',
  //       identifier: 'Oxcead...aBe4',
  //     ),
  //   ]);
  // }
}
